package com.java.base.io.enter;


/**
 * \r 光标移动到本行头
 * \b 回退一个字符
 * \n 换行
 * \t 制表符
 * \033[nA 光标上移n行
 * \03[nB 光标下移n行
 * \033[nC 光标右移n行
 * \033[nD 光标左移n行
 * \033[y;xH 设置光标位置
 * \033[2J 清屏
 * \033[K 清除从光标到行尾的内容
 * \033[s 保存光标位置
 * \033[u 恢复光标位置
 * \033[?25l 隐藏光标
 * \33[?25h 显示光标
 *
 *
 */
public class ConsoleColorFont {

    private final static String PREFIX = "\033[";
    private final static String SUFFIX = PREFIX + "0m";

    public final static int TYPE_DEFAULT = 0;
    public final static int TYPE_HIGH_LIGHT = 1;
    public final static int TYPE_VAGUE = 2;
    public final static int TYPE_ITALIC = 3;
    public final static int TYPE_UNDERLINE = 4;
    public final static int TYPE_TWINKLE_SLOW = 5;
    public final static int TYPE_TWINKLE_FAST = 6;
    public final static int TYPE_CHANGE_COLOR = 7;
    public final static int TYPE_HIDE = 8;

    private final static int FOREGROUND = 30;
    private final static int BACKGROUND = 40;

    public final static int COLOR_BLACK = 0;
    public final static int COLOR_RED = 1;
    public final static int COLOR_GREEN = 2;
    public final static int COLOR_YELLOW = 3;
    public final static int COLOR_BLUE = 4;
    public final static int COLOR_VIOLET = 5;
    public final static int COLOR_CYAN = 6;
    public final static int COLOR_WHITE = 7;

    public static String renders(String s, Integer type, Integer foreground, Integer background) {
        return render(s, type, foreground, background).toString();
    }

    public static String renders(StringBuilder sb, Integer type, Integer foreground, Integer background) {
        return render(sb, type, foreground, background).toString();
    }

    public static StringBuilder render(String s, Integer type, Integer foreground, Integer background) {

        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder(PREFIX);
        if (type != null) {
            sb.append(type);
        }

        if (foreground != null) {
            if (sb.charAt(sb.length() - 1) != '[') {
                sb.append(';');
            }
            sb.append(FOREGROUND + foreground);
        }

        if (background != null) {
            if (sb.charAt(sb.length() - 1) != '[') {
                sb.append(';');
            }
            sb.append(BACKGROUND + background);
        }

        return sb.append('m').append(s).append(SUFFIX);
    }


    public static StringBuilder render(StringBuilder sb, Integer type, Integer foreground, Integer background) {


        if (sb == null) {
            return null;
        }

        int ol = sb.length();

        sb.insert(0, PREFIX);
        if (type != null) {
            sb.insert(sb.length() - ol, type);
        }

        if (foreground != null) {
            if (sb.charAt(sb.length() - ol - 1) != '[') {
                sb.insert(sb.length() - ol, ';');
            }
            sb.insert(sb.length() - ol, FOREGROUND + foreground);
        }

        if (background != null) {
            if (sb.charAt(sb.length() - ol - 1) != '[') {
                sb.insert(sb.length() - ol, ';');
            }
            sb.insert(sb.length() - ol, BACKGROUND + background);
        }

        return sb.insert(sb.length() - ol, 'm').append(SUFFIX);
    }


    public static void main(String[] args) {
//        System.out.print("\033[2J");
        System.out.print("可以\n发送\r消息\n发送");
//        System.out.println("aadsffds \r bb");
//        System.out.println("aadsffds \r\n bb");
//        System.out.println("a\tb");
    }

}
